$.extend({
    gwSons: function(o, i) {
        if (o.children().length == 0) {
            o.html(i)
        } else {
            return arguments.callee(o.children().first(), i)
        }
    },
    cloneWFSons: function($o, $t) {
        var $objs = $o.children();
        var $chtml = $t.children().not($t.children(":first")).remove().andSelf().parent().html();
        $t.html("");
        $objs.each(function() {
            $t.append($chtml)
        })
    },
    zt_lunbo: function(options) {
        var setting = $.extend({
            oImgs: null,
            oTexts: null,
            oCtrl: null,
            ctrlDg: 0,
            oArrow: null,
            oCtrlClass: null,
            Eventer: null,
            sWidth: null,
            visible: 1,
            Timer: 0,
            AutoPlay: 0
        },
        options);
        var $oimg = setting.oImgs;
        var $imgs = $oimg.children();
        var $imgLen = Math.ceil($imgs.size() / setting.visible);
        var $ctrl = null;
        var $ctrlDg = setting.ctrlDg;
        var $text = null;
        var $ArrowL = null;
        var $ArrowR = null;
        var PIMG_INDEX = 0;
        var TIMER = setting.Timer == 0 ? 3000 : setting.Timer;
        var sliderBoolean = !!setting.Eventer ? setting.Eventer == "slider" ? 1 : 0 : 0;
        var sWidth = !!setting.sWidth ? setting.sWidth: $imgs.first().outerWidth();
        var AutoPlay = setting.AutoPlay;
        var INTERVAL = null;
        var timerFun = null;
        var $current_ctrl_class = !!setting.oCtrlClass ? setting.oCtrlClass: "active";
        var arrowFadeIn = {
            "al": function() {
                if ($imgs.is(":animated")) {
                    return false
                };
                PIMG_INDEX--;
                PIMG_INDEX = PIMG_INDEX < 0 ? $imgLen - 1 : PIMG_INDEX;
                _setAction(PIMG_INDEX)
            },
            "ar": function() {
                if ($imgs.is(":animated")) {
                    return false
                };
                PIMG_INDEX++;
                PIMG_INDEX = PIMG_INDEX == $imgLen ? 0 : PIMG_INDEX;
                _setAction(PIMG_INDEX)
            }
        };
        var arrowSlide = {
            "al": function() {
                if ($oimg.is(":animated")) {
                    return false
                };
                PIMG_INDEX--;
                if (PIMG_INDEX == -1) {
                    PIMG_INDEX = $imgLen - 1;
                    $oimg.css({
                        "left": -$imgLen * sWidth
                    })
                };
                _setAction(PIMG_INDEX)
            },
            "ar": function() {
                if ($oimg.is(":animated")) {
                    return false
                };
                PIMG_INDEX++;
                _setAction(PIMG_INDEX)
            }
        };
        var arrowEvents = sliderBoolean ? arrowSlide: arrowFadeIn;
        if (AutoPlay) {
            timerFun = sliderBoolean ? arrowSlide.ar: arrowFadeIn.ar;
            INTERVAL = setInterval(timerFun, TIMER);
            _clearTimerFun($imgs)
        };
        if ( !! setting.oCtrl) {
            $ctrl = setting.oCtrl;
            if (setting.ctrlDg == 1) {
                $.cloneWFSons($oimg, $ctrl);
                if ($ctrlDg) {
                    $ctrl.children().each(function(i) {
                        $.gwSons($(this), i + 1)
                    })
                }
            };
            if (setting.ctrlDg == 2) {
                if ($ctrl.children().size() != $imgs.size()) {
                    alert("the number of img not equal the number of ctrl button!,check it plase")
                }
            };
            $ctrl.children().first().addClass($current_ctrl_class);
            $ctrl.children().mouseover(function() {
                PIMG_INDEX = $(this).index();
                _setAction(PIMG_INDEX)
            });
            if (AutoPlay) {
                _clearTimerFun($ctrl.children())
            }
        };
        if (sliderBoolean) {
            $oimg.css({
                "width": sWidth * $imgLen * 2
            }).andSelf().append($oimg.html()).append($oimg.html())
        };
        if ( !! setting.oTexts) {
            $text = setting.oTexts.children();
            $text.first().show();
            if (AutoPlay) {
                _clearTimerFun($text)
            }
        };
        if ( !! setting.oArrow) {
            $ArrowL = setting.oArrow.al;
            $ArrowR = setting.oArrow.ar;
            $ArrowL.click(arrowEvents.al);
            $ArrowR.click(arrowEvents.ar);
            if (AutoPlay) {
                _clearTimerFun($ArrowL, $ArrowR)
            }
        };
        function initializeCtrl() {
            $ctrl.children().eq(0).attr("class", "size2 border2").siblings().attr("class", "size2 border1")
        };
        function _setAction(uindex) {
            sliderBoolean ? _slideFun(uindex) : _fadeFun(uindex);
            if ( !! $ctrl) {
                $ctrl.children().attr("class", "size2 border1");
                $ctrl.children().eq(uindex == $imgLen ? 0 : uindex).attr("class", "size2 border2")
            }
        };
        function _fadeFun(i) {
            $imgs.eq(i).fadeIn("fast").siblings().fadeOut("fast")
        };
        function _slideFun(step) {
            var nowLeft = -step * sWidth;
            $oimg.animate({
                "left": nowLeft
            },
            800,
            function() {
                if ($oimg.position().left == nowLeft && PIMG_INDEX == $imgLen || PIMG_INDEX > ($imgLen - 1)) {
                    PIMG_INDEX = 0;
                    $oimg.css({
                        "left": 0
                    })
                }
            })
        };
        function _clearTimerFun() {
            $.each(arguments,
            function() {
                $(this).hover(function() {
                    clearInterval(INTERVAL)
                },
                function() {
                    INTERVAL = setInterval(timerFun, TIMER)
                })
            })
        };
        initializeCtrl()
    }
})